package com.donleo.ssm.service.impl;

import com.donleo.ssm.dao.IRoleMapper;
import com.donleo.ssm.dao.ITeamMapper;
import com.donleo.ssm.dao.IUserMapper;
import com.donleo.ssm.model.TeamModel;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.ITeamService;
import com.donleo.ssm.utils.LayTransferResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * @author liangd
 * date 2020-11-25 16:53
 * code
 */
@Service("teamService")
public class TeamServiceImpl implements ITeamService {

    @Resource
    private ITeamMapper teamMapper;

    @Resource
    private IUserMapper userMapper;

    @Resource
    private IRoleMapper roleMapper;

    @Override
    public Integer add(TeamModel teamModel) {
        teamModel.setCreateTime(new Date());
        return teamMapper.add(teamModel);
    }

    @Override
    public Integer delete(Integer id) {
        return teamMapper.delete(id);
    }

    @Override
    public Integer deletes(List<Integer> idsList) {
        return teamMapper.deletes(idsList);
    }

    @Override
    public Integer update(TeamModel teamModel) {
        teamModel.setUpdateTime(new Date());
        return teamMapper.update(teamModel);
    }

    @Override
    public TeamModel findById(Integer id) {
        return teamMapper.findById(id);
    }

    @Override
    public List<TeamModel> findAll(TeamModel teamModel) {
        return teamMapper.findAll(teamModel);
    }

    @Override
    public Integer getCount(TeamModel teamModel) {
        return teamMapper.getCount(teamModel);
    }

    @Override
    public List<UserModel> findAllStudent() {
        return teamMapper.findAllStudent();
    }

    @Override
    public Integer getStudentCount() {
        return teamMapper.getStudentCount();
    }

    /**
     * 分配学生
     *
     * @param list
     * @param id
     * @return
     */
    @Override
    public Integer assignStudent(List<LayTransferResult> list, Integer id) {

        //查询小组名字
        TeamModel teamModel = teamMapper.findById(id);

        List<UserModel> userList = new ArrayList<>();
        //遍历Lay封装类，对userModel赋值
        setLayTransferResult(list, teamModel, userList);
        return teamMapper.assignStudent(userList);
    }

    @Override
    public List<UserModel> findStudentByTeam(Integer id) {
        TeamModel teamModel = teamMapper.findById(id);

        return teamMapper.findStudentByTeam(teamModel.getTeamName());
    }

    /**
     * 统计当前小组人数
     *
     * @param id
     * @return
     */
    @Override
    public Integer getStudentTeamCount(Integer id) {
        TeamModel teamModel = teamMapper.findById(id);
        Integer count = teamMapper.getStudentTeamCount(teamModel.getTeamName());
        if (count != 0) {
            //设置小组人数
            teamModel.setTeamNumber(count);
            teamMapper.update(teamModel);
        }
        return count;
    }

    @Override
    public Integer deleteStudent(List<LayTransferResult> list, Integer id) {
        //查询小组名字
        TeamModel teamModel = teamMapper.findById(id);

        List<UserModel> userList = new ArrayList<>();
        //遍历Lay封装类，对userModel赋值
        setLayTransferResult(list, teamModel, userList);
        return teamMapper.deleteStudent(userList);
    }

    @Override
    public Integer updateTeamLeader(TeamModel teamModel) {
        UserModel userModel = userMapper.findById(teamModel.getUserId());
        teamModel.setTeamLeader(userModel.getUsername());
        return teamMapper.update(teamModel);
    }

    /**
     * 封装
     *
     * @param list
     * @param teamModel
     * @param userList
     */
    private void setLayTransferResult(List<LayTransferResult> list, TeamModel teamModel, List<UserModel> userList) {
        for (LayTransferResult result : list) {
            UserModel userModel = new UserModel();
            userModel.setId(parseInt(result.getValue()));
            userModel.setTeam(teamModel.getTeamName());
            userList.add(userModel);
        }
    }

}
